<?php

namespace App\Models;

class Account extends Base
{

    /**
     * 检查服务号的某个用户是否是认证专员
     * @param type $openid
     * @return boolean
     */
    public function isAudit($openid = '')
    {
        return true;
        //todo
    }

    public function validLogin($data, &$errors)
    {
        $config = [
            'account' => array(
                array('isNotNull', 'account_not_null'),
                array('isAccount', 'account_is_char'),
            ),
            'password' => array(
                array('isNotNull', 'pwd_not_null'),
                array('isPassword', 'pwd_is_char'),
            ),
        ];

        $validator = new \App\Extend\Validator($this->container);
        if (!$result = $validator->validate($config, $data)) {
            $errors = $validator->error;
            $this->language->validator('audit', $errors);
        }
        return $result;
    }

    public function login($data, &$errors)
    {
        $sql = "select * from tb_club_account a where a.account = ? and a.id = ? and a.deleted = 0";
        $one = $this->fetchOne($sql, [$data['account'], $this->club['id']]);
        $validator = new \App\Extend\Validator($this->container);
        if (!$one || $one["password"] != md5($data["password"])) {
            $validator->setError('password', 'login_failure');
            $errors = $validator->error;
            $this->language->validator('audit', $errors);
            return false;
        }
        if ($one['role'] != 3) {
            $validator->setError('account', 'not_auth');
            $errors = $validator->error;
            $this->language->validator('audit', $errors);
            return false;
        }
        $one['auth'] = $one['auth'] ? json_decode($one['auth'], true) : [];
        return $one;
    }

}
